﻿using DataMonitor_IBLL;
using DataMonitor_Tools.Tools;
using DataMonitorMysql.MappingClass;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System.Reflection;

namespace DataMonitorWebApi.Controllers
{
    [Route("[controller]/[action]")]
    [ApiController]
    public class QuartzSupplementController : ControllerBase
    {
        private readonly ILogger<QuartzSupplementController> _logger;
        private readonly IBLL_User _iBLL_User;
        //private readonly string controllerName;
        private readonly IMemoryCache _memoryCache;//内存缓存
        public QuartzSupplementController(ILogger<QuartzSupplementController> logger,  IMemoryCache memoryCache, IBLL_User iBLL_User)
        {
            _logger = logger;
            _memoryCache = memoryCache;
            this._iBLL_User = iBLL_User;
            //controllerName = this.GetType().Name;
        }

        /// <summary>
        /// 获得准确的里程数据（计算路径下所有csv文件的总里程并返回给前端）
        /// </summary>
        /// <param name="jobname"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> GetTotalMileage(string path)
        {
            //var t = MethodBase.GetCurrentMethod();
            //string? methodName = t?.Name;
            var adminprop = _memoryCache.Get<dm_props_admin>("datamonitoradminpara");
            var vehicle=_iBLL_User.LoadEntities(a=>a.watchpath==path).FirstOrDefault()?.vehicle;
            if (adminprop != null && !string.IsNullOrEmpty(vehicle))
            {
                var lastingtime = adminprop.points / adminprop.sample;//csv文件的持续时间
                string watchpath = Path.Combine(path, DateTime.Now.ToString("MM_dd"));
                double sum=await watchpath.GetTotalMileagesfromCSVs(adminprop.reducetimes, lastingtime);
                _memoryCache.Set<double>(vehicle + DateTime.Now.ToString("MM-dd") + "sumdistance", sum);
                return Ok(sum);
            }
            else
            {
                return BadRequest("公共参数没有设置");
            }
        }

        /// <summary>
        /// 获得准确的轨迹数据（计算路径下所有csv文件的总轨迹并返回给前端）
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> GetTotalTracks(string path)
        {
            //var t = MethodBase.GetCurrentMethod();
            //string? methodName = t?.Name;
            var adminprop = _memoryCache.Get<dm_props_admin>("datamonitoradminpara");
            var vehicle = _iBLL_User.LoadEntities(a => a.watchpath == path).FirstOrDefault()?.vehicle;
            if (adminprop != null && !string.IsNullOrEmpty(vehicle))
            {
               
                string watchpath = Path.Combine(path, DateTime.Now.ToString("MM_dd"));
                var gps = await watchpath.GetTotalTracksfromCSVsV2(adminprop.reducetimes);

                return Ok(gps);
                
            }
            else
            {
                _logger.LogWarning($"{vehicle}车辆路径为{path}在复原轨迹时公共参数设置有误");
                return BadRequest("error");
            }
        }
    }
}
